package com.example.phone.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MyEmploymentActivity extends Activity {
	private ListView listEmployment;
	private List<Map<String, Object>> listItems;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_employment_activity);
		listEmployment = (ListView) findViewById(R.id.employmentListView);
		listItems = getData();
		ListEmpViewAdapter adapter = new ListEmpViewAdapter(this);
		listEmployment.setAdapter(adapter);
	}

	private List<Map<String, Object>> getData() {

		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("qiyeName", "焦作三岛输送机械有限公司");

		map.put("job", "汽车司机");

		map.put("interviewTime", "2013-01-23");

		list.add(map);

		map = new HashMap<String, Object>();

		map.put("qiyeName", "焦作市福得机械厂");

		map.put("job", "机械工程师");

		map.put("interviewTime", "2012-12-14");

		list.add(map);

		map = new HashMap<String, Object>();

		map.put("qiyeName", "焦作市滨湖生活广场");

		map.put("job", "商城服务员");

		map.put("interviewTime", "2012-11-02");

		list.add(map);

		map = new HashMap<String, Object>();

		map.put("qiyeName", "焦作市三鑫纸业有限公司");

		map.put("job", "造纸工人");

		map.put("interviewTime", "2011-05-25");

		list.add(map);

		return list;

	}

	static class ListEmpItemView {

		public TextView interviewTime;
		public TextView qiyeName;
		public TextView job;

	}

	public class ListEmpViewAdapter extends BaseAdapter {

		private LayoutInflater listContainer;

		public ListEmpViewAdapter(Context context) {
			listContainer = LayoutInflater.from(context);

		}

		@Override
		public int getCount() {

			return listItems.size();

		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			Map<String, Object> map = (Map<String, Object>) listItems
					.get(position);
			ListEmpItemView holder = null;
			// 如果缓存convertView为空，则需要创建View
			if (convertView == null) {
				holder = new ListEmpItemView();
				// 根据自定义的Item布局加载布局
				convertView = listContainer.inflate(R.layout.one_employment,
						null);
				holder.interviewTime = (TextView) convertView
						.findViewById(R.id.interviewTime);
				holder.qiyeName = (TextView) convertView
						.findViewById(R.id.qiyeName);
				holder.job = (TextView) convertView.findViewById(R.id.job);
				// 将设置好的布局保存到缓存中，并将其设置在Tag里，以便后面方便取出Tag
				convertView.setTag(holder);
			} else {
				holder = (ListEmpItemView) convertView.getTag();
			}
			holder.interviewTime.setText((String) map.get("interviewTime"));
			holder.qiyeName.setText((String) map.get("qiyeName"));
			holder.job.setText((String) map.get("job"));
			return convertView;
		}
	}

}
